from datetime import datetime

from sqlalchemy.orm import Session

from core.entity.do.models import CmsCarTrack
from core.entity.vo.view_models import CmsCarTrackVO


class CmsCarTrackDao:
    """
    车辆轨迹dao
    """

    @classmethod
    def get_cms_car_track_by_id(cls, db: Session, id: int):
        """
        根据id查询高德终端信息
        :param db: orm对象
        :param id: 主键ID
        :return: 高德终端信息对象
        """
        cms_car_track = db.query(CmsCarTrack) \
            .filter(CmsCarTrack.id == id) \
            .first()

        return cms_car_track

    @classmethod
    def get_cms_car_track_by_track_id(cls, db: Session, track_id: str):
        """
        根据终端id查询高德终端信息
        :param db: orm对象
        :param track_id: 终端id
        :return: 高德终端信息对象
        """
        cms_car_track = db.query(CmsCarTrack) \
            .filter(CmsCarTrack.track_id == track_id) \
            .first()

        return cms_car_track

    @classmethod
    def get_cms_car_track_by_gaode_track_id(cls, db: Session, gaode_track_id: str):
        """
        根据终端id查询高德终端信息
        :param db: orm对象
        :param gaode_track_id: 终端id
        :return: 高德终端信息对象
        """
        cms_car_track_list = db.query(CmsCarTrack) \
            .filter(CmsCarTrack.gaode_track_id == gaode_track_id) \
            .all()

        return cms_car_track_list

    @classmethod
    def get_cms_car_track_by_car_number(cls, db: Session, car_number: str, begin_time: datetime, end_time: datetime):
        """
        根据终端id查询高德终端信息
        :param db: orm对象
        :param car_number: 车牌号
        :param begin_time: 开始时间
        :param end_time: 结束时间
        :return: 高德终端信息对象
        """
        cms_car_track_list = db.query(CmsCarTrack) \
            .filter(CmsCarTrack.car_number == car_number and begin_time <= CmsCarTrack.record_time <= end_time) \
            .all()

        return cms_car_track_list

    @classmethod
    def get_cms_car_track_like_name(cls, db: Session,trace_name: str):
        """
        获取所有的参数配置信息
        :param db: orm对象
        :param trace_name: 终端名称
        :return: 高德终端信息对象集合
        """
        cms_car_track_list = db.query(CmsCarTrack) \
            .filter(CmsCarTrack.trace_name.like(f'%{trace_name}%') if trace_name else True) \
            .all()

        return cms_car_track_list

    @classmethod
    def add_cms_car_track_dao(cls, db: Session, cms_car_track: CmsCarTrackVO):
        """
        新增高德终端
        :param db: orm对象
        :param cms_car_track: 高德终端对象
        :return:
        """
        model_dict = CmsCarTrack(**cms_car_track.dict())
        db.add(model_dict)
        db.flush()

        return model_dict

    @classmethod
    def edit_cms_car_track_dao(cls, db: Session, cms_car_track: dict):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param cms_car_track: 需要更新的参数配置字典
        :return:
        """
        db.query(CmsCarTrack) \
            .filter(CmsCarTrack.id == cms_car_track.get('id')) \
            .update(cms_car_track)

    @classmethod
    def delete_by_id_dao(cls, db: Session, id: int):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param id: id
        :return:
        """
        db.query(CmsCarTrack) \
            .filter(CmsCarTrack.id == id) \
            .delete()

    @classmethod
    def delete_by_trace_id_dao(cls, db: Session, gaode_track_id: str):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param id: id
        :return:
        """
        db.query(CmsCarTrack) \
            .filter(CmsCarTrack.gaode_track_id == gaode_track_id) \
            .delete()
